from application.utils.jwt_token import permission_islogin
from fastapi import APIRouter, HTTPException
from application.utils.paginate import paginate
from apps.testapp.model import Test

testapp_router = APIRouter()


@testapp_router.get('/test_get/{id}', summary='详情')
@permission_islogin()
async def test_get(request):
    a = request.state.user  # 当前用户
    # b = request.query_params['li']  # 请求参数DET
    path = request.path_params['id']  # 路径参数DET
    obj = await Test.get(id=path)
    return {'status': 1, 'message': '成功', 'data': obj}


@testapp_router.get('/test_list', summary='列表')
@permission_islogin()
async def test_list(request):
    queryset = Test.all()
    paginated_results = await paginate(queryset, request)
    return {'status': 1, 'message': '成功', 'data': paginated_results.dict()}


@testapp_router.post("/test_post", summary='新增')
@permission_islogin()
async def test_post(request):
    name = request.state.data['name']
    password = request.state.data['password']
    email = request.state.data['email']
    obj = await Test.create(name=name, password=password, email=email)
    return {'status': 1, 'message': '成功', 'data': obj}
